package day1;

/**
 * @author pacai
 * @version 1.0
 */
public class BSAwesome {
    public static int BSAwesom(int[] arr){
        if(arr == null || arr.length == 0) return -1;
        int N = arr.length;

        if(arr[0] < arr[1]) return 0;
        if(arr[N - 1] < arr[N - 2]) return N - 1;
        int L = 0;
        int R = N - 1;
        while(L < R - 1){
            int mid = (L + R) >> 1;
            if(arr[mid] < arr[mid - 1] && arr[mid] < arr[mid + 1]){
                return mid;
            }else{
                if(arr[mid] > arr[mid - 1]){
                    R = mid - 1;
                }else{
                    L = mid + 1;
                }
            }
        }
        return arr[L] < arr[R] ? L : R;
    }

    @SuppressWarnings("all")
    public static boolean check(int[] arr,int minIndex){
        if(arr.length == 0){
            return minIndex == -1;
        }

        int left =  minIndex - 1;
        int right = minIndex + 1;
        boolean leftBigger = left >= 0 ? arr[left] > arr[minIndex] : true;
        boolean rightBigger = right < arr.length ? arr[right] > arr[minIndex] : true;
        return leftBigger && rightBigger;
    }


    public static int[] randomArray(int maxLen, int maxVal){
        int len = (int)(Math.random() * maxLen);
        int[] arr = new int[len];
        if(len > 0){
            arr[0] = (int)(Math.random() * maxVal);
            for (int i = 1; i < len; i++) {
                do{
                    arr[i] = (int)(Math.random() * maxVal);
                }while(arr[i] == arr[i - 1]);
            }
        }
        return arr;
    }

}
